import { TabList, GoodsList } from '../../services/home/home';

Page({
	data: {
		tabList: [],
		goodsList: [],
		goodsListLoadStatus: 0,
		pageLoading: false,
		current: 1,
		autoplay: true,
		duration: 500,
		interval: 5000,
		navigation: { type: 'dots' },
		categoryId: 0,
		tabIndex: '',
		emptyImg: 'https://cdn-we-retail.ym.tencent.com/miniapp/order/empty-order-list.png',
		no_scroll: true,
	},
	goodListPagination: {
		index: 1,
		num: 20,
	},
	privateData: {
		tabIndex: 0,
	},
	onShow() {
		if (this.data.refresh) {
			this.setData({ categoryId: '' })
			this.init(this.data.categoryId);
		}
	},
	onPullDownRefresh() {
		this.setData({ categoryId: this.data.categoryId })
		this.init(this.data.categoryId);
	},

	onLoad(query) {
		let categoryId = query.categoryId
		categoryId = this.data.tabList.map((d) => d.id).includes(categoryId) ? categoryId : ''
		this.init(categoryId);
	},
	init(categoryId) {
		categoryId = categoryId !== undefined ? categoryId : this.data.tabIndex;
		this.loadHomePage(categoryId);
	},
	loadHomePage(categoryId) {
		// 停止当前页面下拉刷新。
		wx.stopPullDownRefresh();
		this.setData({
			pageLoading: true
		});
		//获取tab
		const params = { recommendState: true }
		TabList(params).then((res) => {
			if (res.code == '0000') {
				let cateOne = {
					categoryName: '全部',
					id: 0,
				}
				res.data.unshift(cateOne)
				this.setData({ tabList: res.data, pageLoading: false })
				this.getGoodsList(true, categoryId);
			}
		})

	},
	//获取商品列表
	getGoodsList(fresh = false, categoryId = '') {
		if (fresh) {
			wx.pageScrollTo({
				scrollTop: 0,
			});
		}
		this.setData({ goodsListLoadStatus: 1 });
		const pageSize = this.goodListPagination.num;
		let pageIndex = this.privateData.tabIndex * pageSize + this.goodListPagination.index + 1;
		if (fresh) {
			pageIndex = 1;
		}
		const params = {
			page: {
				size: pageSize,
				current: pageIndex,
			},
			catId1: categoryId
		};
		GoodsList(params).then(res => {
			// this.page.current++;
			if (res.code == '0000') {
				const nextList = res.data.records;
				this.setData({
					goodsList: fresh ? nextList : this.data.goodsList.concat(nextList),
					goodsListLoadStatus: nextList.length > 0 ? 0 : 2,
				});
				this.goodListPagination.index = pageIndex;
				this.goodListPagination.num = pageSize;
			}
		}).catch((err) => {
			this.setData({ goodsListLoadStatus: 3 });
		})
	},

	//上拉加载
	onReachBottom() {
		let categoryId = this.data.categoryId;
		if (this.data.goodsListLoadStatus === 0) {
			// this.setData({ categoryId: this.data.tabIndex })
			this.getGoodsList(false, categoryId);
		}
	},
	//触底事件
	onReTry() {
		this.setData({ current: this.page.current + 1 })
		this.getGoodsList(true);
	},


	//tab切换
	tabChangeHandle(e) {
		const { value } = e.detail;
		this.setData({ categoryId: value })
		this.resetList(value);
	},
	resetList(categoryId = '') {
		this.page = {
			size: this.goodListPagination.index,
			current: this.goodListPagination.num,
		},
			this.setData({ tabIndex: categoryId, goodsList: [] });
		return Promise.all([
			this.getGoodsList(true, categoryId)
		])
	},

	//商品详情
	goodListClickHandle(e) {
		const goodsId = e.detail.goods.id
		wx.navigateTo({
			url: `/pages/goods/details/index?goodsId=${goodsId}`,
		});
	},
	//搜索框跳转
	navToSearchPage() {
		wx.navigateTo({ url: '/pages/goods/search/index' });
	},
	goTop() {
		if (wx.pageScrollTo) {
			wx.pageScrollTo({
				scrollTop: 0
			})
		} else {
			wx.showModal({
				title: '提示',
				content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
			})
		}
	},
	onPageScroll(e) {
		if (e.scrollTop > 600) {
			this.setData({ no_scroll: false })
		} else {
			this.setData({
				no_scroll: true
			})
		}
	}

});
